using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using cn.jpush.api;
using cn.jpush.api.push.mode;
using cn.jpush.api.push.notification;
using cn.jpush.api.common.resp;
using cn.jpush.api.common;
using System.Configuration;

namespace SmartLink.Infrastructure
{
    public static class JPushHelper
    {
        private static readonly string PushUrl = "https://api.jpush.cn/v3/push";
        private static readonly string AppKey = ConfigurationManager.AppSettings["AppKey"];
        private static readonly string MasterSecret = ConfigurationManager.AppSettings["MasterSecret"];

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tags"></param>
        /// <param name="notifyDetail">AlertID,Plate,AlertTypeDesc,StartTime</param>
        /// <returns></returns>
        public static bool PushNotify(List<string> tags,dynamic notifyDetail)
        {
            string notifyContent = "{0}在{1}发生{2}";
            string title = notifyDetail.AlertTypeDesc;
            notifyContent = string.Format(notifyContent, notifyDetail.Plate, notifyDetail.StartTime, notifyDetail.AlertTypeDesc);
            try
            {
				JPushClient client = new JPushClient(AppKey, MasterSecret);
				PushPayload payload = PushObject_android_and_ios(tags, title, notifyContent, notifyDetail);
				var result = client.SendPush(payload);
                if (result.isResultOK())
                    return true;
            }
            catch (APIRequestException e)
            {
                Console.WriteLine("Error response from JPush server. Should review and fix it. ");
                Console.WriteLine("HTTP Status: " + e.Status);
                Console.WriteLine("Error Code: " + e.ErrorCode);
                Console.WriteLine("Error Message: " + e.ErrorMessage);
				throw;
            }
            catch (APIConnectionException e)
            {
                Console.WriteLine(e.Message);
				throw;
            }
            return false;
        }



        public static PushPayload PushObject_android_and_ios(List<string> tags,string title,string notifyContent,dynamic notifyDetail)
        {
            PushPayload pushPayload = new PushPayload();
            pushPayload.platform = Platform.android_ios();
            var audience = Audience.s_tag_and(tags.ToArray());
            pushPayload.audience = audience;

            //alert 通知内容 公用
            Notification notification = new Notification().setAlert(notifyContent);

            //android
            AndroidNotification androidNotification = new AndroidNotification();
            notification.AndroidNotification = androidNotification;
            androidNotification.setTitle(title);
            androidNotification.AddExtra("AlertID", notifyDetail.AlertID);
            androidNotification.AddExtra("Plate", notifyDetail.Plate);

            //ios
            IosNotification iosNotification = new IosNotification();
            notification.IosNotification = iosNotification;
            iosNotification.incrBadge(1);
            iosNotification.AddExtra("AlertID",notifyDetail.AlertID);
            iosNotification.AddExtra("Plate", notifyDetail.Plate);
            pushPayload.notification = notification.Check();
            return pushPayload;
        }
    }
}
